/************************************************************************/
/*Copyright (c) James Zhao 2007,2008
/*This file is part of AdminOverlay.
/*AdminOverlay is free software: you can redistribute it and/or modify
/*it under the terms of the GNU General Public License as published by
/*the Free Software Foundation, either version 3 of the License, or
/*(at your option) any later version.
/*
/*AdminOverlay is distributed in the hope that it will be useful,
/*but WITHOUT ANY WARRANTY; without even the implied warranty of
/*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/*GNU General Public License for more details.
/*
/*You should have received a copy of the GNU General Public License
/*along with AdminOverlay.  If not, see <http://www.gnu.org/licenses/>.                                                                     */
/*************************************************************************/
#pragma once

#include "d3dx9core.h"

#include "CEGUI.h"
using namespace CEGUI;
class GameOverlay;
class GraphicsAdapter
{
public:
	GraphicsAdapter(void);
	void initalize(LPDIRECT3DDEVICE9 device);
	bool isValid();
	void setupDetours();

	void setupEndSceneDetour(int offset, LPCTSTR moduleName);
	void setupResetDetour(int offset, LPCTSTR moduleName);

	bool DisableForward() const { return disableForward; }
	void DisableForward(bool val) { disableForward = val; }
	void SetDrawHandler(void (*drawCallback)(LPDIRECT3DDEVICE9));
	void SetPreResetHandler(void (*resetHandler)());
	void SetPostResetHandler(void (*resetHandler)());
	static GraphicsAdapter *currentInstance;
	~GraphicsAdapter(void);
public:
public:
	int CachedHeight() const { return cachedHeight; }
	int CachedWidth() const { return cachedWidth; }
private:
	bool isDeviceNull;
	bool isDeviceLost;
	int cachedWidth;
	int cachedHeight;
	bool disableForward;
	LPDIRECT3DDEVICE9 device;

private:	
	void (*drawHandler)(LPDIRECT3DDEVICE9);
	void (*preResetHandler)();
	void (*postResetHandler)();

	void draw(LPDIRECT3DDEVICE9);
	static void onWindowResize();
private:
	static HRESULT __stdcall My_EndScene(LPDIRECT3DDEVICE9 device);
	static HRESULT __stdcall My_Reset(LPDIRECT3DDEVICE9, D3DPRESENT_PARAMETERS* pp);
	static HRESULT (__stdcall *endSceneTrampoline)(LPDIRECT3DDEVICE9);
	static HRESULT (__stdcall *resetTrampoline)(LPDIRECT3DDEVICE9, D3DPRESENT_PARAMETERS*);
};